package android.FiltrosJava;
import java.awt.image.*;
import java.awt.Graphics2D;

public abstract class AbstractImageBasic {
		public static double imagem5M = 1024 * 1024 * 5;
		protected String men = "";
		protected boolean exibir = false;

		public static BufferedImage criaImagemRGB(BufferedImage src) {
			return new BufferedImage( src.getWidth(null), src.getHeight(null), BufferedImage.TYPE_3BYTE_BGR);
		}
		
		public BufferedImage createCompatibleDestImage(BufferedImage src) {
	        ColorModel dstCM = src.getColorModel();
	        return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), dstCM.isAlphaPremultiplied(), null);
	    }
		
	   public int[] convertIntColorToVector(int cor, int[] oldData){		   
		   oldData[0] = (int) (cor & 0x00FF0000)>>>16;
		   oldData[1] = (int) (cor & 0x0000FF00)>>>8;
		   oldData[2] = (int) (cor & 0x000000FF);		   
		   return oldData;
	    }
	   
	   public boolean temMensagem(){
		return exibir;   
	   }
	   
	   public String mensagem(){
		   return men;
	   }
	   
	   public abstract BufferedImage applyFilter(BufferedImage src);
	   
	   //public static BufferedImage convertImageToARGB( BufferedImage image ) {
	   public static void convertImageToARGB( BufferedImage image ) {
		   /*
		   if (image.getType() == BufferedImage.TYPE_INT_ARGB )
			   return image;
		   */
		   BufferedImage p = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		   Graphics2D g = p.createGraphics();
		   g.drawImage( image, 0, 0, null );
		   g.dispose();
		   //return p;
		   image = p;
	   }
   	   
	   //public static BufferedImage convertImageToRGB( BufferedImage image ) {
	   public static void convertImageToRGB( BufferedImage image ) {
		   /*
		   if (image.getType() == BufferedImage.TYPE_3BYTE_BGR)
			   return image;
		   */
		   BufferedImage dst = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_3BYTE_BGR);
		   
		   int width = image.getWidth();
		   int height = image.getHeight();    	
		   	    	
		   WritableRaster raster_image = image.getRaster();
		   WritableRaster raster_dst = dst.getRaster();
		   int[] imageData_image = new int[raster_image.getNumBands()];
		   int[] imageData_dst = new int[raster_dst.getNumBands()];
	    	
		   for ( int y = 0; y < height; y++ ) {
			   for ( int x = 0; x < width; x++ ) {
				   raster_image.getPixel(x,y,imageData_image);
				   raster_dst.getPixel(x,y,imageData_dst);
				   imageData_dst[0] = imageData_image[0];
				   imageData_dst[1] = imageData_image[1];
				   imageData_dst[2] = imageData_image[2];				   
				   
				   raster_dst.setPixel(x,y,imageData_dst);    			
			   }
		   }  	    	
		   
		   image = dst;
	   }
	   
	   public static BufferedImage copiaImagem( BufferedImage image ) {		   
		   BufferedImage dst = criaImagemRGB(image);  
		   
		   int width = image.getWidth();
		   int height = image.getHeight();    	
		   	    	
		   WritableRaster raster_image = image.getRaster();
		   WritableRaster raster_dst = dst.getRaster();
		   int[] imageData_image = new int[raster_image.getNumBands()];
		   int[] imageData_dst = new int[raster_dst.getNumBands()];
	    	
		   for ( int y = 0; y < height; y++ ) {
			   for ( int x = 0; x < width; x++ ) {
				   raster_image.getPixel(x,y,imageData_image);
				   raster_dst.getPixel(x,y,imageData_dst);
				   imageData_dst[0] = imageData_image[0];
				   imageData_dst[1] = imageData_image[1];
				   imageData_dst[2] = imageData_image[2];				   
				   
				   raster_dst.setPixel(x,y,imageData_dst);    			
			   }
		   }  	    	
		   
		   return dst;
	   }
	   
	   public int delimitaCor(int c){
		   if (c < 0)
				return 0;
			if (c > 255)
				return 255;
			return c;
	   }
	   
	   public static double negativeMod(double a, double b) {
			int n = (int)(a/b);
			
			a -= n*b;
			if (a < 0)
				return a + b;
			return a;
		}	   
}
